package com.geostar.constructionland.analyse.service.handler.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.geostar.constructionland.analyse.entity.HttpResult;
import com.geostar.constructionland.analyse.entity.alyresult.AlyResult;
import com.geostar.constructionland.analyse.entity.alyresult.AlyResultYpys;
import com.geostar.constructionland.analyse.entity.model.AnalyseModel;
import com.geostar.constructionland.analyse.entity.resulthandler.HandlerResult;
import com.geostar.constructionland.analyse.factory.AdjustModelFactory;
import com.geostar.constructionland.analyse.mapper.sptalyresult.AlyResultYpysMapper;
import com.geostar.constructionland.analyse.service.BaseAdjustService;
import com.geostar.constructionland.analyse.service.handler.IResultHandler;
import com.geostar.constructionland.analyse.service.handler.impl.util.XmlUtil;
import com.geostar.constructionland.common.entity.response.LandInfo;
import com.geostar.constructionland.common.entity.response.model.AdjustModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chengan
 * @description 已批预审分析
 */
@Component
@Slf4j
public class YpysHandler implements IResultHandler {

    @Autowired
    BaseAdjustService adjustService;

    @Autowired
    ObjectMapper objectMapper;

    @Autowired
    AlyResultYpysMapper alyResultYpysMapper;

    @Override
    public HandlerResult handler(AnalyseModel analyseModel, HttpResult httpResult) throws Exception {
        HandlerResult result = HandlerResult.createBySuccess();
        log.info("处理已批预审分析占压返回数据,状态码:{}", httpResult.getCode());
        if (HttpStatus.SC_OK == httpResult.getCode()) {
            String newBody = XmlUtil.parseXml(httpResult.getBody());
            httpResult.setBody(newBody);
            /**保存占压信息**/
            AlyResult alyResult = objectMapper.readValue(httpResult.getBody(), AlyResult.class);
            log.info("body:" + httpResult.getBody());
            if (HttpStatus.SC_OK == alyResult.getStatus()) {
                /**如果成功,构建主体解析**/
                List<AlyResultYpys> alyResultYpyses = objectMapper.readValue(alyResult.getAnalysisResult().getResultRecords(),
                        new TypeReference<List<AlyResultYpys>>() {});
                if(CollectionUtils.isNotEmpty(alyResultYpyses)){

                    /**执行平差**/
                    AdjustModel adjustModel = AdjustModelFactory.getAdjustMod(analyseModel,alyResult);
                    /**平差后的结果**/
                    List<LandInfo> landInfo_adjusted = adjustService.doAjust(analyseModel,adjustModel);

                    /**设置分析标示**/
                    for(AlyResultYpys alyResultYpys : alyResultYpyses){
                        alyResultYpys.setFxbs(alyResult.getAnalyseId());
                        /**更新占压面积**/
                        List<LandInfo> filterLandInfos = landInfo_adjusted.stream().filter(landInfo ->
                                (landInfo.getDkid().equals(alyResultYpys.getDkbs()))).collect(Collectors.toList());
                        if(filterLandInfos.size()>0){
                            alyResultYpys.setZymj(filterLandInfos.get(0).getZymj_adjusted());
                        }
                        alyResultYpysMapper.insert(alyResultYpys);
                    }
                }
            }else{
                throw new Exception(alyResult.getMessage());
            }


        }
        return result;
    }
    }

